home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / rlib / cumprod.r < prev    next >
Text File  |  1994-04-25  |  725b  |  42 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    cumprod ( X )
  4.  
  5. //  Description:
  6.  
  7. //  Compute the cumulative product of a vector or a matrix.
  8.  
  9. //  See Also: prod
  10.  
  11. //-------------------------------------------------------------------//
  12.  
  13. cumprod = function ( x )
  14. {
  15.   local (i, j, m, n, new);
  16.  
  17.   m = x.nr;
  18.   n = x.nc;
  19.  
  20.   new = zeros (m, n);
  21.   if (min ([m, n]) == 1)
  22.   {
  23.     // cumsum on a vector
  24.     new[1] = x[1];
  25.     for (i in 2:max ([m,n]))
  26.     {
  27.       new[i] = x[i] * new[i-1];
  28.     }
  29.   else
  30.     // cumsum on the columns of a matrix
  31.     for (i in 1:n)
  32.     {
  33.       new[1;i] = x[1;i];
  34.       for (j in 2:m)
  35.       {
  36.         new[j;i] = x[j;i] * new[j-1;i];
  37.       }
  38.     }
  39.   }
  40.   return (new);
  41. };
  42.